package phase01.module2.code.question2;

import phase01.module2.code.question2.exceptions.InvalidLocationException;

import java.util.Scanner;

/**
 * 五子棋(Five In a Row)游戏入口
 */
public class FIR {
    private static String getPawnColorLabel(Colors c) {
        String label;
        switch (c) {
            case BLACK:
                label = "黑";
                break;
            case WHITE:
                label = "白";
                break;
            default:
                label = "无";
                break;
        }
        return label;
    }

    public static void main(String[] args) throws InvalidLocationException {
        Board board = new Board();
        Coordinate coordinate = new Coordinate();
        boolean won = false;
        String playerAName;
        String playerBName;
        int playerAAge;
        int playerBAge;
        int playerALevel;
        int playerBLevel;
        Colors playerAColor;
        Colors playerBColor;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入玩家A的姓名、年龄、段位：");
        playerAName = sc.next();
        playerAAge = sc.nextInt();
        playerALevel = sc.nextInt();
        System.out.println("请输入玩家A的选择棋子颜色：");
        System.out.println("1. 黑色");
        System.out.println("2. 白色");
        playerAColor = (sc.nextInt() == 1) ? Colors.BLACK : Colors.WHITE;
        System.out.print("请输入玩家B的姓名、年龄、段位：");
        playerBName = sc.next();
        playerBAge = sc.nextInt();
        playerBLevel = sc.nextInt();
        playerBColor = (playerAColor == Colors.WHITE) ? Colors.BLACK : Colors.WHITE;
        Player a = new Player(playerAName, playerAAge, playerALevel, new Pawn(playerAColor, "x"), board);
        Player b = new Player(playerBName, playerBAge, playerBLevel, new Pawn(playerBColor, "o"), board);

        // 落子
        System.out.println("游戏开始：玩家A - " + a.getName() + "(执" + getPawnColorLabel(a.getPawn().getColor()) + "棋) VS. 玩家B - " + b.getName() + "(执" + getPawnColorLabel(b.getPawn().getColor()) + "棋)");
        System.out.println("=================================================================================================================");
        String turnFlag = "A";
        int pawnX = 0;
        int pawnY = 0;
        while (!won && board.isFullyOccupied()) {
            // 游戏开始
            board.draw();
            if ("A".equals(turnFlag)) {
                System.out.print("玩家A - " + a.getName() + "请输入落子坐标：");
                while (true) {
                    pawnX = sc.nextInt();
                    pawnY = sc.nextInt();
                    coordinate.setX(pawnX);
                    coordinate.setY(pawnY);
                    try {
                        won = a.play(coordinate);
                        break;
                    } catch (InvalidLocationException e) {
                        System.out.print("无效的落子，请重新输入合法的落子坐标：");
                    }
                }
                if (won) {
                    System.out.println("游戏结束，玩家A - " + a.getName() + "胜出！玩家等级：" + a.getLevel());
                } else {
                    turnFlag = "B";
                }
            } else {
                System.out.print("玩家B - " + b.getName() + "请输入落子坐标：");
                while (true) {
                    pawnX = sc.nextInt();
                    pawnY = sc.nextInt();
                    coordinate.setX(pawnX);
                    coordinate.setY(pawnY);
                    try {
                        won = b.play(coordinate);
                        break;
                    } catch (InvalidLocationException e) {
                        System.out.print("无效的落子，请重新输入合法的落子坐标：");
                    }
                }
                if (won) {
                    System.out.println("游戏结束，玩家B - " + b.getName() + "胜出！玩家等级：" + b.getLevel());
                } else {
                    turnFlag = "A";
                }
            }
        }
    }
}
